package com.hxf.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dto.AdminRoleDto;
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.RestaurantAdminRole;
import com.hxf.dto.RestaurentUserDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.RoleDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.user.RestaurantUserService;
import com.hxf.web.vo.UserRoleVo;

@Repository("restaurantUserService")
public class RestaurantUserServiceImpl extends BaseServiceImpl implements RestaurantUserService {
	
	public ReturnData<RestaurentUserDto> findByRestaurentUser(RestaurentUserDto userDto, int targetPage, int pageSize)throws ServiceException{
		try{
			return super.daoFacade.getRestaurantUserDao().findByRestaurentUser(userDto, targetPage, pageSize);
		}catch(DaoLevelException e){
			e.printStackTrace();
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	public void updateUserStatus(String userIds,Integer status) throws ServiceException{
		try {
			RestaurentUserDto ud = null;
			 if (!"".equals(userIds) && null != userIds) {
				  String userId [] = userIds.split(",");
				  if (null != userId) {
					  for (int i = 0;i < userId.length; i++) {
						  ud = (RestaurentUserDto)super.daoFacade.getRestaurantUserDao().get(RestaurentUserDto.class, Integer.parseInt(userId[i]));
						  ud.setStatus(status);
						  super.daoFacade.getRestaurantUserDao().update(ud);
					  }
				  }
			 } 
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public void saveUserAddRole(RestaurantAdminRole userDto,String roleIds) throws ServiceException {
		try {
			//删除当前用户绑定角色
			super.daoFacade.getRestaurantUserDao().deleteUserRoleByUserId(userDto.getUserId()); 
			
			RestaurantAdminRole urDto = null;
			if (PublicUtil.isNotNullAndEmpty(roleIds)) {
				String [] ids = roleIds.split(",");
				for (int i = 0;i < ids.length; i++) {
					urDto = new RestaurantAdminRole();
					urDto.setUserId(userDto.getUserId());
					urDto.setRoleId(Integer.parseInt(ids[i])); 
					super.daoFacade.getRestaurantUserDao().create(urDto);//插入角色权限
				}
			}
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	public List<UserRoleVo> findByUserAddRole(RestaurentUserDto urDto,Integer userId) throws ServiceException{
		try {
			  List<RoleDto> roleList = super.daoFacade.getRoleDao().findByRole(null);
			  List<RoleDto> selectRoleList = super.daoFacade.getRestaurantUserDao().findByUserRole(urDto);//查询用户权限 要设置的用户
			  //UserDto userDto = super.daoFacade.getUserDao().selectUserOrAdmin(userId);
			  RoleDto rd = null;
			  RoleDto srd = null;
			  UserRoleVo urVo = null;
			  List<UserRoleVo> urList = new ArrayList<UserRoleVo>();
			  //上级为空,为超级管理员，显示所有角色
			 // if(null !=userDto && (null == userDto.getParentId()||"null".equals(userDto.getParentId()))
			//		  && (null==userDto.getParentUserId() || "null".equals(userDto.getParentUserId()))){
				  
				  if (roleList != null && roleList.size() > 0) {
					  for (int i = 0;i < roleList.size(); i++) {
						  rd = roleList.get(i);
						  urVo = new UserRoleVo();
						  
						  //if(rd.getRoleId()==3){ //先只显示餐厅人员权限
							  urVo.setRoleId(rd.getRoleId());
							  urVo.setRoleName(rd.getRoleName());
							  urVo.setRemark(rd.getRemark());
							  urVo.setStatus(rd.getStatus());
							  urVo.setSelectStatus(false);
							  if (selectRoleList != null && selectRoleList.size() > 0) {
								  for (int j = 0;j < selectRoleList.size(); j++) {
									  srd = selectRoleList.get(j);
									  if (rd.getRoleId() == srd.getRoleId()) {
										  urVo.setSelectStatus(true);
									  }
								  }
							  } 
							  urList.add(urVo); 
						  //}
					  }
				  }
				  return urList;
			  /*}else{//不是超级管理员,只显示餐厅原本给的权限
				  urDto.setUserId(userId); //设置为 当前餐厅登陆的 ID
				  List<RoleDto> selectRoleListParent =  super.daoFacade.getUserDao().findByUserRole(urDto);
				  if(null != selectRoleList && selectRoleList.size()>0){
					  for(int i=0;i<selectRoleListParent.size();i++){
						  rd = selectRoleListParent.get(i);
						  urVo = new UserRoleVo();
						  urVo.setRoleId(rd.getRoleId());
						  urVo.setRoleName(rd.getRoleName());
						  urVo.setRemark(rd.getRemark());
						  urVo.setStatus(rd.getStatus());
						  urVo.setSelectStatus(false);
						  if(null != selectRoleList && selectRoleList.size()>0){
							  for(int j=0;j<selectRoleList.size();j++){
								  srd = selectRoleList.get(j);
								  if (rd.getRoleId() == srd.getRoleId()) {
									  urVo.setSelectStatus(true);//已经有权限
								  }
							  }
						  }
						  urList.add(urVo); 
					  }
					  return urList;
				  }
				  return null;
			  }*/
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}  
	}

	@Override
	public int restaurantUserLogin(RestaurentUserDto restaurentUserDto)
			throws ServiceException {
		 try {
			return super.daoFacade.getRestaurantUserDao().restaurantUserLogin(restaurentUserDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	@Override
	public RestaurentUserDto findRestaurentUser(RestaurentUserDto dto) throws ServiceException{
		try {
			
			return super.daoFacade.getRestaurantUserDao().findRestaurentUser(dto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
}
